<?php

namespace app\beautiful\controller\admin;

use app\BaseController;
use app\common\model\ProduceDepartModel;
use app\common\model\ProduceModel;


class Producedepart extends BaseController{

    public function list(){
        $limit=$this->request->limit;
        $page=$this->request->page;
        $name= $this->request->param('name');
        $type_id = $this->request->param('type_id');
        $origin_type = $this->request->param('origin_type',1);
        $where=[];
        $shop_id=$this->request->shopId;
        $where['shop_id']=$shop_id;
        $where['origin_type']=$origin_type;
        $order['a.id']='desc';
        if(!empty($name)){
            $where['name']=$name;
        }
        if(!empty($type_id)){
            $where['type_id']=$type_id;
        }
        $res_data=ProduceDepartModel::list($where,$order,$page,$limit);
        return $this->success($res_data);
    }
    public function info(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('ID错误');
        }
        $where['id']=$id;
        $res_data=ProduceDepartModel::info($where);
        return $this->success($res_data);
    }
    public function effect(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('ID错误');
        }
        $is_effect=$this->request->param('is_effect',null);
        if($is_effect===null){
            return $this->error('is_effect参数错误');
        }
        $where['id']=$id;
        $up['is_effect']=$is_effect;
        $up['edit_user_id']=$this->request->accountId;
        $res=ProduceDepartModel::up($up,$where);
        return $this->success();
    }
    public function del(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('ID错误');
        }
        $is_delete=1;
        $where['id']=$id;
        $info=ProduceDepartModel::info($where);
        if(empty($info)){
            return $this->error('数据不存在');
        }
        $up['is_delete']=$is_delete;
        $up['edit_user_id']=$this->request->accountId;;
        $res=ProduceDepartModel::up($up,$where);
        if(!$res){
            return $this->error('删除失败');
        }
        $p_s['depart_id']=$id;
        $p_up['is_delete']=1;
        ProduceModel::up($p_up,$p_s);
        return $this->success();
    }
    public function edit(){
        $shop_id=$this->request->shopId;
        $id = $this->request->param('id',0);
        $name = $this->request->param('name',null);
        $remark=$this->request->param('remark',null);
        $type_id=$this->request->param('type_id',null);
        $print_ids=$this->request->param('print_ids',null);
        $is_effect=$this->request->param('is_effect',null);
        $notice_type=$this->request->param('notice_type',null);
        if($id==0){
            return $this->error('id错误');
        }
        $ProduceDepartModel= new ProduceDepartModel();
        $all_info=$ProduceDepartModel->where(['id'=>$id])->find();
        if(empty($all_info)) return $this->error('数据不存在');
        $s=[];
        $s[]=['name','=',$name];
        $s[]=['shop_id','=',$shop_id];
        $s[]=['id','<>',$id];
        $s[]=['is_delete','=',0];
        $info=$ProduceDepartModel->where($s)->find();
        if(!empty($info)) return $this->error('数据重复');
        $up=[];
        $up['edit_user_id']=$this->request->accountId;
        if($name!=null) $up['name']=$name;
        if($remark!=null) $up['remark']=$remark;
        if($type_id!=null) $up['type_id']=$type_id;
        if($print_ids!=null) $up['print_ids']=trim($print_ids,',');
        if($notice_type!=null) $up['notice_type']=trim($notice_type,',');
        if($is_effect!=null) $up['is_effect']=$is_effect;
        $where['id']=$id;
        $res_data=ProduceDepartModel::up($up,$where);
        return $this->success($res_data);
    }
    public function add(){
        $shop_id=$this->request->shopId;
        $name = $this->request->param('name',null);
        $remark=$this->request->param('remark',null);
        $type_id=$this->request->param('type_id',null);
        $print_ids=$this->request->param('print_ids',null);
        $is_effect=$this->request->param('is_effect',null);
        $notice_type=$this->request->param('notice_type',null);
        $origin_type=$this->request->param('origin_type',1);
        $ProduceDepartModel= new ProduceDepartModel();
        $s=[];
        $s[]=['name','=',$name];
        $s[]=['shop_id','=',$shop_id];
        $s[]=['is_delete','=',0];
        $all_info=$ProduceDepartModel->where($s)->find();
        if(!empty($all_info)) return $this->error('数据重复');
        $print_ids=trim($print_ids,',');
        $insert=compact('shop_id','remark','type_id','print_ids','name','origin_type','notice_type');
        if($is_effect!==null) $insert['is_effect']=$is_effect;
        $insert['create_user_id']=$this->request->accountId;
        $insert['add_time']=date('Y-m-d H:i:s');
        $insert['edit_time']=date('Y-m-d H:i:s');
        $insert_id=ProduceDepartModel::insertGetId($insert);
        return $this->success($insert_id);
    }

}

